package shuaige.jdk.集合.映射表集合Map;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class 使用PropertiesMap {
    public static void main(String[] args) throws IOException {
        从classpath读取属性文件1();
    }

    /**
     * 用Properties读取配置文件，一共有三步：
     * 创建Properties实例；
     * 调用load()读取文件；
     * 调用getProperty()获取配置。
     */
    public static void 从classpath读取属性文件1() {
        Properties props = new Properties();
        try {
            URL resource = 使用PropertiesMap.class.getResource("/setting.properties");
            props.load(resource.openStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filepath = props.getProperty("last_open_file");
        String interval = props.getProperty("auto_save_interval", "120");
        System.out.println(filepath);
        System.out.println(interval);
    }

    public static void 从classpath读取属性文件_指定编码() {
        Properties props = new Properties();
        try {
            URL resource = 使用PropertiesMap.class.getResource("/setting.properties");
            props.load(new FileReader(resource.getFile(), StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filepath = props.getProperty("last_open_file");
        String interval = props.getProperty("auto_save_interval", "120");
        String z1 = props.getProperty("z1");
        System.out.println(filepath);
        System.out.println(interval);
        System.out.println(z1);
    }

    public static void 读取多个文件_后读取的会覆盖前面属性() {
        Properties props = new Properties();
        try {
            props.load(使用PropertiesMap.class.getResourceAsStream("/setting.properties"));
            props.load(使用PropertiesMap.class.getResourceAsStream("/setting1.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filepath = props.getProperty("last_open_file");
        String interval = props.getProperty("auto_save_interval", "120");
        System.out.println(filepath);
        System.out.println(interval);
    }

    public static void 从内存读取一个字节流属性() {
        String settings = "# test" + "\n" + "course=Java" + "\n" + "last_open_date=2019-08-07T12:35:01";
        ByteArrayInputStream input = new ByteArrayInputStream(settings.getBytes(StandardCharsets.UTF_8));
        Properties props = new Properties();
        try {
            props.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("course: " + props.getProperty("course"));
        System.out.println("last_open_date: " + props.getProperty("last_open_date"));
        System.out.println("last_open_file: " + props.getProperty("last_open_file"));
        System.out.println("auto_save: " + props.getProperty("auto_save", "60"));
    }

    public static void 写入配置文件() {
        Properties props = new Properties();
        props.setProperty("url", "http://www.liaoxuefeng.com");
        props.setProperty("language", "Java");
        try {
            URL resource = 使用PropertiesMap.class.getResource("/settingWrite.properties");
            System.out.println("写入class：" + resource.getFile());
            props.store(new FileOutputStream(resource.getFile()), "这是写入的properties注释");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
